package org.bravo369.inflightxt;

import org.bravo369.inflightxt.utils.Globals;
import org.bravo369.inflightxt.utils.Exitable;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class SettingsView extends Activity implements Exitable {

  private class CollectionRateListener implements OnItemSelectedListener {

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int pos,
                               long id) {
      Globals globals = Globals.getInstance();
      String item = (String) parent.getItemAtPosition(pos);
      globals.dataRefreshRate = 1000 / Integer.parseInt(item);
    }

    @Override
    public void onNothingSelected(AdapterView<?> view) {
      Globals globals = Globals.getInstance();
      globals.dataRefreshRate = 1000;
    }
  }

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.settings_view);

    Globals.getInstance().addExitHandler(this);

    addCollectionRates();

  }

  private void addCollectionRates() {
    Spinner collectionRateSpinner =
      (Spinner) findViewById(R.id.collectionRateDropdown);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
        this, R.array.collectionRateArray,
        android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(
        android.R.layout.simple_spinner_dropdown_item);
    collectionRateSpinner.setAdapter(adapter);
    collectionRateSpinner.setOnItemSelectedListener(
        new CollectionRateListener());
  }

  @Override
  public void onExit() {
    finish();
  }
}
